package com.example.demo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThreadTest {

    private static final Logger LOG = LoggerFactory.getLogger(ThreadTest.class);

    public static void main(String[] args) throws Exception {

        //通过参数获取请求地址
        String url = args.length > 0 ? args[0] : "http://localhost:8080/";

        //通过参数获取间隔时间
        int thread = args.length > 1 ? Integer.parseInt(args[1]) : 10;

        //用线程池发送请求
        ExecutorService cachedThreadPool = new ThreadPoolExecutor(thread, thread, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

        int i = 1;
        while (i <= thread) {
            int finalI = i;
            cachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    LOG.info("Hello:" + ThreadApp.hello);
                    Instant start = Instant.now();
                    ThreadApp.hello = "AAA" + finalI;
                    LOG.info(DateFormat.getDateTimeInstance().format(new Date()) + "---Result: " + ThreadApp.hello + "---NO: " + finalI + "---ThreadID: " + Thread.currentThread().getId()
                        + "---ThreadName: " + Thread.currentThread().getName() + "---Duration: " + Duration.between(start, Instant.now()).toMillis() + "ms");
                }
            });
            LOG.info(DateFormat.getDateTimeInstance().format(new Date()) + "---LoopNO: " + i);
            i++;

            // 按线程休眠
            //            Thread.sleep(1000 / thread);
        }
    }

    @Test
    public void test() throws ParseException {
        DateFormat oldDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(calcDaysBetween(new Date(), oldDate.parse("2024-05-09 23:59:59")) + " 天");
        System.out.println(getDaysBetween(new Date(), oldDate.parse("2024-05-09 23:59:59")) + " 天");
    }

    private int calcDaysBetween(Date currentDate, Date oldDate) {
        Instant start = Instant.now();
        Calendar d1 = new GregorianCalendar();
        d1.setTime(oldDate);
        Calendar d2 = new GregorianCalendar();
        d2.setTime(currentDate);
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        LOG.info("Duration: " + Duration.between(start, Instant.now()).toMillis() + "ms");
        return days;
    }

    private int getDaysBetween(Date currentDate, Date oldDate) {
        Instant start = Instant.now();
        LocalDate date1 = LocalDate.of(oldDate.getYear(), oldDate.getMonth(), oldDate.getDay());
        LocalDate date2 = LocalDate.of(currentDate.getYear(), currentDate.getMonth(), currentDate.getDay());
        long days = ChronoUnit.DAYS.between(date1, date2);
        LOG.info("Duration: " + Duration.between(start, Instant.now()).toMillis() + "ms");
        return (int) days;
    }
}
